
/**
 * @Title: TaskController.java
 * @Package org.car.world.controller
 * @Description: TODO(用一句话描述该文件做什么)
 * @author dell
 * @date 2018年11月14日
 * @version V1.0
 */

package org.car.world.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.car.world.common.util.Page;
import org.car.world.core.entily.TaskTable;
import org.car.world.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName: TaskController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author dell
 * @date 2018年11月14日
 *
 */
@Controller
@RequestMapping("admin")
public class TaskController {
    @Autowired
    TaskService taskService;

    @RequestMapping("totasklist")
    public String gotoTaskList(@RequestParam(value = "form", required = false, defaultValue = "1") Integer form,
                               Model model) {
        Page<TaskTable> page = new Page<TaskTable>();
        page.setCurrentPage(form);
        page.setPageSize(2);
        page.setTotalRow(taskService.selectcount());
        page.setPageList(taskService.selectLimit(page.getCurrentPage(), page.getPageSize()));
        model.addAttribute("page", page);
        return "application_list";
    }

    @RequestMapping("toupdatetask")
    public String gotoTaskUpdate(Integer taskId, Model model) {
        TaskTable task = taskService.selectByPrimaryKey(taskId);
        model.addAttribute("task", task);
        return "application_up";
    }

    @RequestMapping("doupdatetask")
    public String doTaskUpdate(TaskTable task, Model model, MultipartFile a_logoPicPath, HttpServletRequest request,
                               HttpServletResponse response) throws IllegalStateException, IOException {
        int flag = 0;
        String path = null;
        String json = "";
        String fPath = "";
        String fileName = "";
        if (!a_logoPicPath.isEmpty()) {
            if (a_logoPicPath != null) {
                fPath = "templates\\upload\\";
                String type = null;
                fileName = a_logoPicPath.getOriginalFilename();
                System.err.println("文件原名称" + fileName);
                type = fileName.indexOf(".") != -1
                        ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
                if (type != null) {
                    if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase())
                            || "JPG".equals(type.toUpperCase())) {
                        String realPath = request.getSession().getServletContext().getRealPath("/");
                        String trueFileName = fileName;
                        path = realPath + fPath + trueFileName;
                        System.err.println("存放图片文件的路径:" + path);
                        a_logoPicPath.transferTo(new File(path));
                        System.err.println("文件成功上传到指定目录下");
                    }
                    json = "{\"res\":1}";
                } else {
                    System.out.println("不是我们指定的文件类型，请重试");
                    json = "{\"res\":0}";
                }
            }
        }
        task.setTaskPicture(fPath + fileName);
        flag = taskService.updateByPrimaryKey(task);
        if (flag > 0) {
            return "totasklist";
        } else {
            return "toupdatetask";
        }
    }

    @RequestMapping("deltaskpic")
    @ResponseBody
    public Boolean doDelTaskPic(Integer upid) {
        int flag = taskService.updateByPrimaryKeyPic(upid);
        if (flag > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("dodeltask")
    @ResponseBody
    public Boolean doDeleteTask(Integer taskId) {
        int flag = taskService.deleteByPrimaryKey(taskId);
        if (flag > 0) {
            return true;
        } else {
            return false;
        }
    }

    @RequestMapping("doaddtask")
    public String doAddTask(TaskTable task, Model model, MultipartFile a_logoPicPath, HttpServletRequest request,
                            HttpServletResponse response) throws IllegalStateException, IOException {
        int flag = 0;
        String path = null;
        String json = "";
        String fPath = "";
        String fileName = "";
        if (!a_logoPicPath.isEmpty()) {
            if (a_logoPicPath != null) {
                fPath = "templates\\upload\\";
                String type = null;
                fileName = a_logoPicPath.getOriginalFilename();
                System.err.println("文件原名称" + fileName);
                type = fileName.indexOf(".") != -1
                        ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
                if (type != null) {
                    if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase())
                            || "JPG".equals(type.toUpperCase())) {
                        String realPath = request.getSession().getServletContext().getRealPath("/");
                        String trueFileName = fileName;
                        path = realPath + fPath + trueFileName;
                        System.err.println("存放图片文件的路径:" + path);
                        a_logoPicPath.transferTo(new File(path));
                        System.err.println("文件成功上传到指定目录下");
                    }
                    json = "{\"res\":1}";
                } else {
                    System.out.println("不是我们指定的文件类型，请重试");
                    json = "{\"res\":0}";
                }
            }
        }
        task.setTaskPicture(fPath + fileName);
        flag = taskService.insert(task);
        if (flag > 0) {
            return "totasklist";
        } else {
            return "doaddtask";
        }
    }

    @RequestMapping("toaddtask")
    public String gotoAddTask() {
        return "application_add";
    }
}
